<template>
  <div class="main-container">
    <el-col style="margin-bottom: 20px">
      <el-button type="primary" @click="goToAdd">添加分类</el-button>
    </el-col>
    <el-table :data="cateList" style="width: 100%; margin-bottom: 20px">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序" align="center" width="80">
        <template slot-scope="scope">
          <el-input v-model="scope.row.sort" @blur="changeSort(scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="分类名称" width="200" align="center"> </el-table-column>
      <el-table-column prop="count" label="站点数量" width="200" align="center"> </el-table-column>
      <el-table-column label="添加时间" align="center">
        <template slot-scope="scope">
          <div>
            {{ scope.row.add_time | timeFormat('yy-mm-dd hh:MM:ss') }}
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="is_show" label="是否显示" align="center" width="100">
        <template slot-scope="scope">
          <div>
            <el-switch v-model="scope.row.is_show" active-value="1" inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @change="setCateStatus(scope.row.id, scope.row.is_show)"> </el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <div>
            <el-link type="primary" :underline="false" @click="goToAddSite(scope.row.id)">添加站点</el-link>
            <el-divider direction="vertical"></el-divider>
            <el-link type="primary" :underline="false" @click="goToEdit(scope.row.id)">编辑</el-link>
            <el-divider direction="vertical"></el-divider>
            <el-link type="primary" :underline="false" @click="remove(scope.row.id)">删除</el-link>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="total"></pagination>
  </div>
</template>

<script>
import { getCateList, setCateStatus, editCate } from '@/api/category'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      cateList: [],
      total: 0,
    }
  },
  created() {
    // this.getCateList()
  },
  methods: {
    async getCateList() {
      const res = await getCateList()
      if (res.code != 1) return this.$message.error(res.msg)
      // this.$message.success(res.msg)
      // console.log(res)
      this.cateList = res.data.data
      this.total = res.data.total
    },
    goToAdd(row) {
      this.$router.push('/addCategory')
    },
    goToAddSite(pid) {
      this.$router.push({
        path: '/addSite',
        query: {
          pid,
        },
      })
    },
    goToEdit(id) {
      this.$router.push({
        path: '/editCategory',
        query: {
          id,
        },
      })
    },
    remove(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.setCateStatus(id, 2)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    async setCateStatus(id, type) {
      const res = await setCateStatus({
        id,
        type,
      })
      if (res.code != 1) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      this.getCateList()
    },
    async changeSort(row) {
      const data = {
        id: row.id,
        sort: row.sort,
      }
      const res = await editCate(data)
      if (res.code != 1) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      this.getCateList()
    },
    handleSizeChange(newSize) {
      this.queryInfo.page_size = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.getCateList()
    },
  },
  computed: {
    ...mapGetters({
      imgUrl: 'settings/imgUrl',
    }),
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-table__row {
  height: 60px !important;
}
</style>
